package com.cqie.threadlocal;

/**
 * ThreadLocal为每个线程单独提供一份存储空间
 * 将ThreadLocal进行封装，便于代码的维护和迭代
 */
public class UserThreadLocal {
    /**
     * 存储任意类型的对象
     */
    private static ThreadLocal<Object> userThread = new ThreadLocal<>();

    public static void set(Object obj) {
        userThread.set(obj);
    }

    @SuppressWarnings("unchecked")
    public static <T> T get(Class<T> type) {
        Object obj = userThread.get();
        if (type.isInstance(obj)) {
            return type.cast(obj);
        } else {
            return null; // 或者抛出一个异常，取决于你希望如何处理类型不匹配的情况
        }
    }

    public static void remove() {
        userThread.remove();
    }
}